package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bo.ReasonEstimateBo;
import vo.CauseVo;
import vo.StackTraceVo;
import dao.hibernate.CauseVoDAO;
import dao.hibernate.StackTraceVoDAO;

import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StackTracePatternServlet extends HttpServlet {
	private static Logger LOGGER = LoggerFactory.getLogger(StackTracePatternServlet.class);
	
	/**
	 * Constructor of the object.
	 */
	public StackTracePatternServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		LOGGER.debug("doGet"  );
		String stacktraceUid = request.getParameter("stacktraceUid");
		LOGGER.debug("stacktraceUid: "+stacktraceUid);
		Object tmp = request.getSession().getAttribute("stackTraceVo");
		StackTraceVo vo = null;
		boolean findAgain =false;
		if(tmp != null){
			 vo = (StackTraceVo)tmp;
			 if(!vo.getUid().toString().equals(stacktraceUid)){
				 findAgain =true;
			 }else {
				 findAgain =false ;
			 }
		}else{
			 findAgain =true;
			
			
		}
		if(findAgain){
			StackTraceVoDAO dao = new StackTraceVoDAO();
			vo = dao.findById(Integer.valueOf(stacktraceUid.trim()));
			if(NumberUtils.isDigits(vo.getKeyCause())){
				CauseVo data=new CauseVoDAO().findById(Integer.valueOf(vo.getKeyCause()));
				vo.setKeyCause(data.getKeyCause());
				
			}
			
			
			request.getSession().setAttribute("stackTraceVo", vo);
		}
	    final	String line = ReasonEstimateBo.estimateV01(vo);
	    request.getSession().setAttribute("stackTraceEstReason", line);
		RequestDispatcher dispatcher = request.getRequestDispatcher("popupbasic.jsp");
	    dispatcher.forward(request, response); 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(final HttpServletRequest request, final HttpServletResponse response)
			throws ServletException, IOException {
		LOGGER.debug("doPost"  );
		String stacktraceUid = request.getParameter("stacktraceUid");
		LOGGER.debug("stacktraceUid: "+stacktraceUid);		
		RequestDispatcher dispatcher = request.getRequestDispatcher("popupbasic.jsp");
	    dispatcher.forward(request, response);
	    
		
	}

}
